﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace agent
{
    class Communicate
    {
        /// <summary>
        /// 临时文件名
        /// </summary>
        static string tmpfile = "../../log/tmp/tmp_cmt_agent";
        /// <summary>
        /// 接收信息进程名
        /// </summary>
        static string procRecvName = "CommRecvAgent.exe";
        /// <summary>
        /// 发送信息进程名
        /// </summary>
        static string procSendName = "CommSendAgent.exe";
        Process procRecv;
        private int tmpfileNO;
        private int time;
        private byte msgLevel;
        private byte hostMap;
        private byte[] data;

        public Communicate()
        {
            this.tmpfileNO = 0;

            this.procRecv = new Process();
            this.procRecv.StartInfo.CreateNoWindow = true;
            this.procRecv.StartInfo.FileName = Communicate.procRecvName;
            this.procRecv.StartInfo.WorkingDirectory = ".";
            this.procRecv.StartInfo.UseShellExecute = false;
            this.procRecv.Start();
        }

        public bool SendMSG(DateTime time, MsgLevel lv, byte map, string data)
        {
            this.time = (Int32)((time.Ticks - new DateTime(1970, 1, 1, 8, 0, 0).Ticks) / 10000000);
            this.msgLevel = Convert.ToByte(Convert.ToInt32(lv.ToString("d")));
            this.hostMap = map;
            this.data = Encoding.ASCII.GetBytes(data);

            Process p = new Process();

            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.Arguments = this.time.ToString() + " " + this.msgLevel.ToString() + " " + this.hostMap.ToString() + " " +
                                            Encoding.ASCII.GetString(this.data);
            p.StartInfo.FileName = Communicate.procSendName;
            p.StartInfo.WorkingDirectory = ".";
            p.StartInfo.UseShellExecute = false;
            p.Start();

            return true;
        }

        public bool RecvMSG(out DateTime time, out MsgLevel lv, out byte map, out string data)
        {
            string strLine;
            StreamReader reader;

            time = DateTime.Now;
            lv = MsgLevel.LV_CMT;
            map = 0;
            data = "";

            if (File.Exists(Communicate.tmpfile + this.tmpfileNO.ToString()) != true)
            {
                return false;
            }
            else
            {
                reader = new StreamReader(Communicate.tmpfile + this.tmpfileNO.ToString());

                strLine = reader.ReadLine();
                if (strLine == null)
                {
                    return false;
                }
                lv = (MsgLevel)Enum.Parse(typeof(MsgLevel), strLine);

                strLine = reader.ReadLine();
                if (strLine == null)
                {
                    return false;
                }
                map = Convert.ToByte(strLine);

                strLine = reader.ReadLine();
                if (strLine == null)
                {
                    return false;
                }
                data = strLine;

                this.time = (Int32)((time.Ticks - new DateTime(1970, 1, 1, 8, 0, 0).Ticks) / 10000000);
            }

            this.tmpfileNO++;
            return true;
        }

        ~Communicate()
        {
            if (this.procRecv.HasExited == false)
            {
                this.procRecv.Kill();
            }
        }
    }
}
